package day0722;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;

public class Test4 {
    public static void main(String[] args) {
        JPanel p = new JPanel();
        p.setLayout(null);
        JLabel l = new JLabel(new ImageIcon("images/背景.jpg"));
        l.setLocation(0, 0);
        l.setSize(480, 800);
        p.add(l);
        ArrayList<JButton> btns = creatBtn();
        System.out.println(btns.size());
        Collections.shuffle(btns);
        putBtns(btns, p, 20, 40, 7, 7, 0);  //49+1
        putBtns(btns, p, 50, 80, 1, 2, 49);
        cheakCover(btns);
        
        ArrayList<JButton> through = new ArrayList<>();
        addClick(btns, through,p);
        JFrame f = new JFrame("羊了个羊");
        f.setSize(480, 800);
        f.add(p);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    private static void addClick(ArrayList<JButton> btns, ArrayList<JButton> bottom,JPanel p) {
        for (int i = 0; i < btns.size(); i++) {
            JButton btn = btns.get(i);
            btn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    btns.remove(btn);
                    int index = -1;
                    String same = "no";
                    for (int j = 0; j < bottom.size(); j++) {
                        String bottomBtnName = bottom.get(j).getName();
                        String btnName = btn.getName();
                        if (bottomBtnName.equals(btnName)) {
                            index = j;
                            same = "yes";
                            break;
                        }
                    }
                    if (bottom.size() >= 7) {
                        JOptionPane.showMessageDialog(null, "游戏结束");
                        return;
                    }
                    if (same.equals("yes")) {
                        bottom.add(index, btn);
                    } else {
                        bottom.add(btn);
                    }
                    for (int j = 0; j < bottom.size(); j++) {
                        bottom.get(j).setLocation(j * 60 + 27, 640);
                    }
                    ActionListener lis=btn.getActionListeners()[0];///////////////////
                    btn.removeActionListener(lis);//////////////////
                   for (int j = 0; j <= bottom.size()-3; j++) {
                        if ((bottom.get(j).getName()).equals(bottom.get(j+2).getName())){
                            /*bottom.remove(bottom.get(j));
                            bottom.remove(bottom.get(j));
                            bottom.remove(bottom.get(j));*/
                            JButton delBtn1=bottom.remove(j);
                            p.remove(delBtn1);
                            JButton delBtn2=bottom.remove(j);
                            p.remove(delBtn2);
                            JButton delBtn3=bottom.remove(j);
                            p.remove(delBtn3);
                            /*for (int k = 0; k < bottom.size(); k++) {
                                System.out.println(bottom.get(k).getName());
                            }  */
                        }
                    }

                    cheakCover(btns);

                   //bottom.get(j).setLocation(j * 60 + 27, 640);
                    for (int j = 0; j < bottom.size(); j++) {
                        JButton btn=bottom.get(j);
                        btn.setLocation(j * 60 + 27, 640);
                    }
                }
            });
        }
    }




    private static void cheakCover(ArrayList<JButton> btns) {

        boolean isCover=false;
        for (int i = 0; i < btns.size() - 24; i++) {
            JButton btn = btns.get(i);
            int x1 = btn.getX() - 59;
            int x2 = btn.getX() + 59;

            int y1 = btn.getY() + 66;
            int y2 = btn.getY() - 66;
            for (int j = i + 1; j < btns.size(); j++) {
                JButton top = btns.get(j);
                int topx = top.getX();
                int topy = top.getY();


                if (x1 < topx & topx < x2 & y2 < topy & topy < y1) {
                    isCover=true;
                    btn.setEnabled(false);
                }


            }

            if(isCover==false){
                btn.setEnabled(true);
            }

        }



        System.out.println(btns.get(0).isEnabled());
    }





    private static void putBtns(ArrayList<JButton> btns, JPanel p, int x, int y, int row, int col, int start) {
        for (int i = 0; i < col * row; i++) {
            JButton btn = btns.get(start + i);
            btn.setSize(59, 66);
            btn.setLocation(i % col * 59 + x, i / col * 66 + y);
            p.add(btn, 0);
        }
    }

    private static ArrayList<JButton> creatBtn() {
        String[] a = new String[]{"兔子", "刷子", "剪刀", "南瓜", "奶瓶", "帽子", "干草", "手套", "月饼", "树", "树桩", "栗子",
                "毛线", "水桶", "火堆", "白菜", "耙子", "苹果", "草", "萝卜", "蜜蜂", "酒坛", "铃铛", "面具"};
        ArrayList<JButton> list = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 24; j++) {//24  *2   48
                JButton btn = new JButton(new ImageIcon("images/" + a[j] + ".png"));
                btn.setDisabledIcon(new ImageIcon("images/" + a[j] + "2.png"));
                btn.setName(a[j]);
                list.add(btn);
            }
        }
       JButton btn = new JButton(new ImageIcon("images/兔子.png"));
        btn.setDisabledIcon(new ImageIcon("images/兔子2.png"));
        btn.setName("兔子");
        list.add(btn);


        JButton btn2 = new JButton(new ImageIcon("images/刷子.png"));
        btn2.setDisabledIcon(new ImageIcon("images/刷子2.png"));
        btn2.setName("刷子");
        list.add(btn2);

        JButton btn3 = new JButton(new ImageIcon("images/刷子.png"));
        btn3.setDisabledIcon(new ImageIcon("images/刷子2.png"));
        btn3.setName("刷子");
        list.add(btn3);


        return list;
    }
}
